package licode.unisop.client.utils;

import licode.unisop.client.info.SopAccessInfo;
import licode.unisop.client.info.SopCodeInfo;
import licode.unisop.client.info.SopTokenInfo;
import licode.unisop.client.vo.SopClient;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 根据请求获取认证相关的参数（客户和用户请求信息）
 *
 * @author WR
 */
public class SopRequestParse {
    public static String getValue(HttpServletRequest req, String key) {
        return getValueOrDefault(req, key, "");
    }

    public static String getValueOrDefault(HttpServletRequest req, String key, String defVal) {
        if (null == req) {
            return "";
        }

        String val = req.getHeader(key);
        if (StringUtil.isBlank(val)) {
            val = req.getParameter(key);
            if (StringUtil.isBlank(val)) {
                val = findCookieValue(req, key);
            }
        }

        if (StringUtil.isBlank(val)) {
            val = defVal;
        }

        return val;
    }

    private static String findCookieValue(HttpServletRequest req, String name) {
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie != null && name.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public static String getAccessToken(HttpServletRequest req) {
        return getValue(req, SopAuthConst.ACCESS_TOKEN);
    }

    public static String getModalToken(HttpServletRequest req) {
        return getValue(req, SopAuthConst.MODAL_TOKEN);
    }

    public static String getPlatformId(HttpServletRequest req) {
        return getValue(req, SopAuthConst.PLATFORM_TOKEN);
    }

    public static String getRealmId(HttpServletRequest req) {
        return getValueOrDefault(req, SopAuthConst.REALM_ID, "default");
    }

    public static String getTenantId(HttpServletRequest req) {
        return getValueOrDefault(req, SopAuthConst.TENANT_ID, "default");
    }

    public static String getClientId(HttpServletRequest req) {
        String terminal = getValue(req, SopAuthConst.CLIENT_ID);
        return terminal.isEmpty() ? getValue(req, SopAuthConst.CLIENT_ID_0) : terminal;
    }

    public static String getClientSecret(HttpServletRequest req) {
        String terminal = getValue(req, SopAuthConst.CLIENT_SECRET);
        return terminal.isEmpty() ? getValue(req, SopAuthConst.CLIENT_SECRET_0) : terminal;
    }

    public static String getTerminalId(HttpServletRequest req) {
        String terminal = getValue(req, SopAuthConst.TERMINAL_ID);
        return terminal.isEmpty() ? getValue(req, SopAuthConst.TERMINAL_ID_0) : terminal;
    }

    public static String getPlatformSecret(HttpServletRequest req) {
        return getValue(req, SopAuthConst.PLATFORM_SECRET);
    }

    public static String getRefreshToken(HttpServletRequest req) {
        return getValue(req, SopAuthConst.REFRESH_TOKEN);
    }

    public static String getTicketUrl(HttpServletRequest req) {
        return getValue(req, SopAuthConst.TICKET_URL);
    }

    public static String getTicket(HttpServletRequest req) {
        return getValue(req, SopAuthConst.CODE);
    }

    public static String getCaptchaKey(HttpServletRequest req) {
        return getValue(req, SopAuthConst.CAPTCHA_KEY);
    }

    public static String getCaptchaCode(HttpServletRequest req) {
        return getValue(req, SopAuthConst.CAPTCHA_CODE);
    }

    public static SopClient getClientDto(HttpServletRequest req) {
        return SopClient.builder().clientId(getValue(req, SopAuthConst.CLIENT_ID))
                .clientSecret(getValue(req, SopAuthConst.CLIENT_SECRET))
                .scope(getValue(req, SopAuthConst.SCOPE))
                .state(getValue(req, SopAuthConst.STATE))
                .build();
    }

    public static SopTokenInfo getAccessTokenInfo(HttpServletRequest req) {
        return SopTokenInfo.builder()
                .realmId(getRealmId(req))
                .token(getAccessToken(req))
                .platformId(getPlatformId(req))
                .build();
    }

    public static SopTokenInfo getRefreshTokenInfo(HttpServletRequest req) {
        return SopTokenInfo.builder()
                .realmId(getRealmId(req))
                .token(getRefreshToken(req))
                .platformId(getPlatformId(req))
                .build();
    }

    public static SopCodeInfo getCodeInfo(HttpServletRequest req) {
        return SopCodeInfo.builder()
                .code(getTicket(req))
                .state(getValue(req, SopAuthConst.STATE))
                .platformId(getPlatformId(req))
                .ipAddr(IPUtils.getIpAddr(req))
                .userBrowser(IPUtils.getUserAgent(req))
                .build();
    }

    public static SopAccessInfo getSsoAuthToken(HttpServletRequest req) {
        return SopAccessInfo.builder()
                .realmId(getRealmId(req))
                .clientId(getClientId(req))
                .clientSecret(getClientSecret(req))
                .tenantId(getTenantId(req))
                .terminalId(getTerminalId(req))
                .category(getValue(req, SopAuthConst.CATEGORY))
                .ticketUrl(getTicketUrl(req))
                .username(getValue(req, SopAuthConst.USERNAME))
                .password(getValue(req, SopAuthConst.PASSWORD))
                .redirectUrl(getValue(req, SopAuthConst.REDIRECT_URL))
                .code(getTicket(req))
                .state(getValue(req, SopAuthConst.STATE))
                .grantType(getValue(req, SopAuthConst.GRANT_TYPE))
                .platformId(getPlatformId(req))
                .captchaKey(getValue(req, SopAuthConst.CAPTCHA_KEY))
                .captchaCode(getValue(req, SopAuthConst.CAPTCHA_CODE))
                .ipAddr(IPUtils.getIpAddr(req))
                .userBrowser(IPUtils.getUserAgent(req))
                .build();
    }
}
